package com.spzx.product.controller;

import com.spzx.common.core.web.domain.AjaxResult;
import com.spzx.common.core.web.page.TableDataInfo;
import com.spzx.common.log.annotation.Log;
import com.spzx.common.log.enums.BusinessType;
import com.spzx.product.domain.Product;
import com.spzx.product.service.IProductService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;
import com.spzx.common.core.web.controller.BaseController;

import java.util.List;


/**
 * <p>
 * 商品 前端控制器
 * </p>
 *
 * @author xiongkunYang
 * @since 2025/05/26 18:30
 */
@Tag(name = "商品")
@RestController
@RequestMapping("/product")
@RequiredArgsConstructor
public class ProductController extends BaseController {

    private final IProductService productService;

    @Operation(summary = "查询商品列表")
    @GetMapping("/list")
    public TableDataInfo list() {
        super.startPage();
        return super.getDataTable(productService.productList());
    }

    @Operation(summary = "新增商品")
    @Parameters({
            @Parameter(name = "product", description = "商品信息")
    })
    @Log(title = "商品", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Product product) {
        return super.toAjax(productService.saveProduct(product));
    }

    @Operation(summary = "更新上下架状态")
    @Parameters({
            @Parameter(name = "id", description = "商品id"),
            @Parameter(name = "status", description = "状态")
    })
    @Log(title = "商品", businessType = BusinessType.UPDATE)
    @GetMapping("/updateStatus/{id}/{status}")
    public AjaxResult updateStatus(@PathVariable Long id,
                                   @PathVariable Integer status) {
        if (status != 1 && status != -1) {
            return AjaxResult.error("status值非法");
        }
        return toAjax(productService.updateStatus(id, status));
    }

    @Operation(summary = "删除商品")
    @Parameters({
            @Parameter(name = "ids", description = "商品id列表")
    })
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable List<Long> ids) {
        return toAjax(productService.removeProductByIds(ids));
    }
}
